/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.entity.StoreCustomerNotify;
import com.chlitina.store.modules.expert.service.StoreCustomerNotifyService;
import com.chlitina.store.modules.sms.entity.LogStoreMsg;
import com.chlitina.store.modules.sms.entity.SmsStoreGroupLog;
import com.chlitina.store.modules.sms.entity.SmsSysCustomer;
import com.chlitina.store.modules.sms.service.SmsStoreGroupLogService;
import com.chlitina.store.modules.sms.service.SmsSysCustomerService;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 店面短信统计Controller
 * 
 * @author lvbo
 * @version 2013-06-21
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/sms/logStoreMsg")
public class LogStoreMsgController extends BaseController {

	@Autowired
	private SmsStoreGroupLogService smsStoreGroupLogService;

	@Autowired
	private SmsSysCustomerService smsSysCustomerService;
	
	@Autowired
	private StoreCustomerNotifyService storeCustomerNotifyService;
	@ModelAttribute
	public LogStoreMsg get(@RequestParam(required = false) Long id) {
		return new LogStoreMsg();
	}

	@RequestMapping(value = "list")
	public String list(LogStoreMsg logStoreMsg, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Store store = StoreUtils.getStore();
		HttpSession session = request.getSession();
		String start = request.getParameter("startDate");
		String end = request.getParameter("endDate");
		String oldStart = (String)session.getAttribute("startDate");
		String oldEnd = (String)session.getAttribute("endDate");
		start = start != null ? start : oldStart;
		end = end != null ? end : oldEnd;
		if (store!=null && start != null && end != null) {
			List<SmsStoreGroupLog> groupMsgList = smsStoreGroupLogService.findByParameter(store.getId(), start, end);
			Long realGroupMsgNum = smsStoreGroupLogService.getRealCount(store.getId(), start, end);
			Long sendGroupMsgNum = smsStoreGroupLogService.getSendCount(store.getId(), start, end);
			
			List<SmsSysCustomer> allAutoMsgList = smsSysCustomerService.findByParameter(store.getId(), start, end);
			
			Long realBirthdayMsgNum = smsSysCustomerService.getRealCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_BIRTHDAY);
			Long sendBirthdayMsgNum = smsSysCustomerService.getSendCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_BIRTHDAY);
			
			Long realHolidayMsgNum = smsSysCustomerService.getRealCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_HOLIDAY);
			Long sendHolidayMsgNum = smsSysCustomerService.getSendCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_HOLIDAY);
			
			Long realNewStoreMsgNum = smsSysCustomerService.getRealCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_NEW_STORE);
			Long sendNewStoreMsgNum = smsSysCustomerService.getSendCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_NEW_STORE);
			
			List<StoreCustomerNotify> notifyMsgList = storeCustomerNotifyService.findByParameter(store.getId(), start, end);
			
			Long realNotifyMsgNum = storeCustomerNotifyService.getRealCount(store.getId(), start, end);
			Long sendNotifyMsgNum = storeCustomerNotifyService.getSendCount(store.getId(), start, end);
			
			long realOthers = 0L;
			if (realBirthdayMsgNum == null) {
				realBirthdayMsgNum = 0L;
			}
			if (realHolidayMsgNum == null) {
				realHolidayMsgNum = 0L;
			}
			realOthers = realBirthdayMsgNum + realHolidayMsgNum;
			
			long sendOthers = 0L;
			if (sendBirthdayMsgNum == null) {
				sendBirthdayMsgNum = 0L;
			}
			if (sendHolidayMsgNum == null) {
				sendHolidayMsgNum = 0L;
			}
			sendOthers = sendBirthdayMsgNum + sendHolidayMsgNum;
			logStoreMsg.setStore(store);
			logStoreMsg.setAllAutoMsgList(allAutoMsgList);
			logStoreMsg.setGroupMsgList(groupMsgList);
			logStoreMsg.setNotifyMsgList(notifyMsgList);
			session.setAttribute("startDate", start);
			session.setAttribute("endDate", end);
			model.addAttribute("realGroupMsgSize", realGroupMsgNum == null ? "0" : realGroupMsgNum);
			model.addAttribute("sendGroupMsgSize", sendGroupMsgNum == null ? "0" : sendGroupMsgNum);
			model.addAttribute("realNotifyMsgSize", realNotifyMsgNum == null ? "0" : realNotifyMsgNum);
			model.addAttribute("sendNotifyMsgSize", sendNotifyMsgNum == null ? "0" : sendNotifyMsgNum);
			model.addAttribute("realOtherAutoMsgSize", realOthers);
			model.addAttribute("sendOtherAutoMsgSize", sendOthers);
			model.addAttribute("realNewsStoreMsgSize", realNewStoreMsgNum == null ? "0" : realNewStoreMsgNum);
			model.addAttribute("sendNewsStoreMsgSize", sendNewStoreMsgNum == null ? "0" : sendNewStoreMsgNum);
			session.setAttribute("logStoreMsg", logStoreMsg);
		}
		return "modules/sms/logStoreMsgList";
	}
	@RequestMapping(value = "remain")
	public String remain(LogStoreMsg logStoreMsg, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Store store = StoreUtils.getStore();
		String start = request.getParameter("startDate");
		String end = request.getParameter("endDate");
		if (store!=null && start != null && end != null) {
			Long groupMsgNum = smsStoreGroupLogService.getRealCount(store.getId(), start, end);
			Long notifyMsgNum = storeCustomerNotifyService.getRealCount(store.getId(), start, end);
			Long birthdayMsgNum = smsSysCustomerService.getRealCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_BIRTHDAY);
			Long holidayMsgNum = smsSysCustomerService.getRealCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_HOLIDAY);
			
			Long sendGroupMsgNum = smsStoreGroupLogService.getSendCount(store.getId(), start, end);
			Long sendNotifyMsgNum = storeCustomerNotifyService.getSendCount(store.getId(), start, end);
			Long sendBirthdayMsgNum = smsSysCustomerService.getSendCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_BIRTHDAY);
			Long sendHolidayMsgNum = smsSysCustomerService.getSendCount(store.getId(), start, end, SmsSysCustomer.MESSAGE_TYPE_HOLIDAY);
			long allReal = 0L;
			if (groupMsgNum == null) {
				groupMsgNum = 0L;
			}
			if (notifyMsgNum == null) {
				notifyMsgNum = 0L;
			}
			if (birthdayMsgNum == null) {
				birthdayMsgNum = 0L;
			}
			if (holidayMsgNum == null) {
				holidayMsgNum = 0L;
			}
			allReal = birthdayMsgNum + holidayMsgNum + groupMsgNum + notifyMsgNum;
			
			long allSend = 0L;
			if (sendGroupMsgNum == null) {
				sendGroupMsgNum = 0L;
			}
			if (sendNotifyMsgNum == null) {
				sendNotifyMsgNum = 0L;
			}
			if (sendBirthdayMsgNum == null) {
				sendBirthdayMsgNum = 0L;
			}
			if (sendHolidayMsgNum == null) {
				sendHolidayMsgNum = 0L;
			}
			allSend = sendBirthdayMsgNum + sendHolidayMsgNum + sendGroupMsgNum + sendNotifyMsgNum;
			int remainSms = store.getSms_remain_amount();
			model.addAttribute("startDate", start);
			model.addAttribute("endDate", end);
			model.addAttribute("allRealSmsSize", allReal);
			model.addAttribute("allSendSmsSize", allSend);
			model.addAttribute("remainSms", remainSms);
		}
		return "modules/sms/storeSmsRemain";
	}
	
	@RequestMapping(value = "detail")
	public String detail(@RequestParam(value="type")String type, LogStoreMsg logStoreMsg, HttpServletRequest request, HttpServletResponse response, Model model) {
		logStoreMsg = (LogStoreMsg)request.getSession().getAttribute("logStoreMsg");
		model.addAttribute("logStoreMsg", logStoreMsg);
		if ("group".equals(type)) {
			return "modules/sms/storeGroupMsgDetail";
		}
		if ("notify".equals(type)) {
			return "modules/sms/storeNotifyMsgDetail";
		}
		if ("other".equals(type)) {
			model.addAttribute("title", "生日和节假日提示短信详情");
			model.addAttribute("type", SmsSysCustomer.MESSAGE_TYPE_BIRTHDAY +
					"," + SmsSysCustomer.MESSAGE_TYPE_HOLIDAY);
		}
		if ("store".equals(type)) {
			model.addAttribute("title", "新店开业提示短信详情");
			model.addAttribute("type", SmsSysCustomer.MESSAGE_TYPE_NEW_STORE);
		}
		return "modules/sms/logStoreMsgDetail";
	}
}
